//
//  DMConfirmOrderVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/16.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMConfirmOrderVC.h"
#import "DMConfirmOrderFirstCell.h"
#import "DMConfirmOrderSecondCell.h"
#import "DMSelectPayWayVC.h"
#import "DMAddressListVC.h"
//model
#import "KStoreItemDetailModel.h"
#import "KSureOrderModel.h"
//request
#import "KStoreRequest.h"


@interface DMConfirmOrderVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *priceLb;

@property (nonatomic,strong)KMineAddressModel * addressModel;


@end

@implementation DMConfirmOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    
    self.navigationItem.title = @"确认订单";
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMConfirmOrderFirstCell" bundle:nil] forCellReuseIdentifier:@"DMConfirmOrderFirstCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMConfirmOrderSecondCell" bundle:nil] forCellReuseIdentifier:@"DMConfirmOrderSecondCell"];
    self.priceLb.text = [NSString stringWithFormat:@"¥%@",self.orderModel.totalSalePrice];
    self.orderModel.addrView = self.addressModel;
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 88;
//        self.tableView.rowHeight = UITableViewAutomaticDimension;
//        self.tableView.estimatedRowHeight = 100;
//        return self.tableView.rowHeight;
    }
    return 138;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DMConfirmOrderFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMConfirmOrderFirstCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMConfirmOrderFirstCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.noAddressView.hidden = NO;
        cell.hadAddressView.hidden = YES;
        
        if (self.addressModel) {
            cell.noAddressView.hidden = YES;
            cell.hadAddressView.hidden = NO;
            cell.nameLb.text = self.addressModel.person;
            cell.addressLb.text = [NSString stringWithFormat:@"%@%@%@ %@",self.addressModel.prName,self.addressModel.ciName,self.addressModel.arName,self.addressModel.addr];
            cell.phoneLb.text = self.addressModel.mobile;
        }else{
            cell.noAddressView.hidden = NO;
            cell.hadAddressView.hidden = YES;
            
        }
        return cell;
    }
    
    
    DMConfirmOrderSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMConfirmOrderSecondCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMConfirmOrderSecondCell" owner:self options:nil].firstObject;
    }
    
    if (self.orderModel.saleSkuViews.count>0) {
        KStoreItemDetailSkuModel * skuModel = self.orderModel.saleSkuViews[0];
        
        cell.nameLb.text = skuModel.name;
        cell.sizeLb.text = [NSString stringWithFormat:@"规格:%@",skuModel.attrOpValues];
        cell.numLb.text =[NSString stringWithFormat:@"数量: %@",skuModel.qty];
    
    }

    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DMAddressListVC *vc = [DMAddressListVC new];
    MJWeakSelf
    [vc setSelectedBlock:^(KMineAddressModel *addressModel) {
        weakSelf.addressModel = addressModel;
        
        [weakSelf.tableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

//确认下单
- (IBAction)payClick:(UIButton *)sender {
    

    if (self.addressModel == nil) {
        [JLProgressHUDManager showErrorHud:@"请选择收货地址"];
        return;
    }
    
    
    NSMutableArray * skuArr = [NSMutableArray array];
    NSMutableArray * qtyArr = [NSMutableArray array];
    
    NSMutableArray * skuviewArr = [NSMutableArray array];
    
    for (KStoreItemDetailSkuModel * skuModel in self.orderModel.saleSkuViews) {
        
        [skuArr addObject:skuModel.id];
        [qtyArr addObject:skuModel.qty];
        
        NSMutableDictionary * skuviewDic = [NSMutableDictionary dictionary];
        [skuviewDic setValue:skuModel.id forKey:@"id"];
        [skuviewDic setValue:skuModel.qty forKey:@"qty"];
        [skuviewArr addObject:skuviewDic];
    }
    
    //
    NSMutableDictionary * jsonDic = [NSMutableDictionary dictionary];
    
    [jsonDic setValue:self.addressModel.id forKey:@"addrId"];

    [jsonDic setValue:skuviewArr forKey:@"saleSkuViews"];

    [jsonDic setValue:@"2" forKey:@"type"];
    
    NSString * jsonStr = [jsonDic jsonStringEncoded];
    
    KStoreRequest * request = [[KStoreRequest alloc]initStoreSureOrderWithAddrId:self.addressModel.id carItemIds:nil id:nil leaveWord:nil qty:nil soEntityJsons:jsonStr groupId:nil type:@"2"];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager hideHud];
        KSureOrderModel * orderModel = [KSureOrderModel modelWithJSON:baseModel.data];
        DMSelectPayWayVC *vc = [DMSelectPayWayVC new];
        vc.entityid = orderModel.id;
        vc.payPrice = orderModel.total.doubleValue;
        [weakSelf.navigationController pushViewController:vc animated:YES];

        [[NSNotificationCenter defaultCenter]postNotificationName:KCreateStoreOrderNotiName object:nil userInfo:nil];

        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        
    }];
    
    

}



@end
